#include <huniplacer_3d/ProgressBar.h>

#include <GL/gl.h>
#include <GL/freeglut.h>

#include <huniplacer_3d/PanelPart.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <cstring>

ProgressBar::ProgressBar(unsigned int x, unsigned int y, unsigned int width, unsigned int height, float r, float b, float g, float bg_r, float bg_g, float bg_b, double value, double max_value, char* value_type, char* name) :
					PanelPart(name, x, y, width, height, r, b, g, bg_r, bg_g, bg_b, value_type) {
	current_value = value;
	this->max_value = max_value;
}

void ProgressBar::draw() {
	int screen_height = glutGet(GLUT_SCREEN_HEIGHT);
	
	//Background
	set_color3f(bg_color_r, bg_color_g, bg_color_b);
	glBegin(GL_QUADS);
		glVertex2i(x, screen_height - y - height);
		glVertex2i(x + width, screen_height - y - height); 
		glVertex2i(x + width, screen_height - y); 
		glVertex2i(x, screen_height - y);
	glEnd();
	
	if (max_value != 0) {
		//Foreground progress
		set_color3f(color_r, color_g, color_b);
		glBegin(GL_QUADS);
			glVertex2i(x, screen_height - y - height);
			glVertex2i(x + (width * (current_value/ max_value)), screen_height - y - height); 
			glVertex2i(x + (width * (current_value/ max_value)), screen_height - y); 
			glVertex2i(x, screen_height - y);
		glEnd();
	}
	
	set_color3f(1.0, 1.0, 1.0);
	
	std::ostringstream progressInfo;
	if (max_value != 0) {
		progressInfo 	<< initial_name
							<< " progress: " 
							<< current_value/ max_value*100 
							<< value_type;
	} else {
	progressInfo 	<< initial_name
							<< " progress: " 
							<< "0"
							<< value_type;
	}
	
	name = progressInfo.str();
}
